package com.ruoyi.web.controller.workflow;

import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.flowable.core.domain.ProcessQuery;
import com.ruoyi.workflow.domain.vo.WfDefinitionVo;
import com.ruoyi.workflow.domain.vo.WfDeployVo;
import com.ruoyi.workflow.domain.vo.WfTaskVo;
import com.ruoyi.workflow.service.IWfCopyService;
import com.ruoyi.workflow.service.IWfProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api("工作流流程管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/process")
public class WfProcessController {
    private final IWfProcessService processService;
    private final IWfCopyService copyService;

    /**
     * 查询可发起流程列表
     *
     * @param processQuery
     * @param pageQuery    分页参数
     * @return
     */
    @ApiOperation("发起流程列表")
    @GetMapping("/list")
    public TableDataInfo<WfDefinitionVo> startProcessList(ProcessQuery processQuery, PageQuery pageQuery) {
        return processService.selectPageStartProcessList(processQuery, pageQuery);
    }

    /**
     * 查询流程部署关联表单信息
     *
     * @param definitionId 流程定义id
     * @param deployId     流程部署id
     * @return
     */
    @GetMapping("/getProcessForm")
    public R<?> getForm(@RequestParam("definitionId") String definitionId,
                        @RequestParam("deployId") String deployId,
                        @RequestParam(value = "procInsId", required = false) String procInsId
    ) {
        return R.ok(processService.selectFormContent(definitionId, deployId, procInsId));
    }

    /**
     * 总结
     *
     * @param processDefId 流程定义id
     * @param variables    流程变量id
     * @return
     */
    @PostMapping("/start/{processDefId}")
    public R<Void> start(@PathVariable("processDefId") String processDefId, @RequestBody Map<String, Object> variables) {
        processService.startProcessByDefId(processDefId, variables);
        return R.ok();
    }

    /**
     * 我拥有的流程
     *
     * @param processQuery
     * @param pageQuery
     * @return
     */
    @GetMapping("/ownList")
    public TableDataInfo<WfTaskVo> ownProcessList(ProcessQuery processQuery, PageQuery pageQuery) {
        return processService.selectPageOwnProcessList(processQuery, pageQuery);
    }

    /**
     * 删除流程
     * @param instanceIds 流程实例ID串
     * @return
     */
    @DeleteMapping("/instance/{instanceIds}")
    public R<Void> delete(@PathVariable String[] instanceIds)
    {
        processService.deleteProcessByIds(instanceIds);
        return R.ok();
    }

    /**
     * 获取待办列表
     * @param processQuery
     * @param pageQuery
     * @return
     */
    @GetMapping("/todoList")
    public TableDataInfo<WfTaskVo> todoProcessList(ProcessQuery processQuery,PageQuery pageQuery)
    {
        return processService.selectPageTodoProcessList(processQuery,pageQuery);
    }

    /**
     * 查询流程详情信息
     * @param procInsId
     * @param taskId
     * @return
     */
    @GetMapping("/detail")
    public R<WfDeployVo> detail(String procInsId, String taskId)
    {
        return R.ok(processService.queryProcessDetail(procInsId,taskId));
    }


}
